
package com.borqs.music.tasks;

import static com.borqs.music.util.MusicConf.ReqType.MUSIC_GET_SETTINGS;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.http.RequestMethod;
import com.borqs.music.util.MusicConf;
import com.borqs.music.util.MusicConf.ServerSettingNames;

public class QuerySettingsTask extends Task implements ServerSettingNames {
    private String mName;
    private String mTicket;

    public QuerySettingsTask(TaskListener listener, String ticket, String name) {
        super(listener);
        mTicket = ticket;
        mName = name;
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("name", mName);
        
        final String URL = MusicConf.getUrl(MUSIC_GET_SETTINGS);
        HttpRequest request = new HttpRequest(RequestMethod.GET, URL, mTicket, params);
        return request;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        JSONObject jObj = resp.asJSONObject();

        return jObj.getInt(mName);
    }
}
